package states
{
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.system.fscommand;
	/**
	 * ...
	 * @author Leong Shi Yun
	 */
	public class MainMenu implements IState
	{
		//private const/var
		private var m_stage:Stage = null;
		private var m_mainMenuSprite:MainMenuSprite = null;
		
		/* public functions */
		public function MainMenu()
		{
			
		}
		
		public function initialize(theStage:Stage):void
		{
			if (theStage)
			{
				m_stage = theStage;
				m_stage.focus = m_stage;
			}
			
			if (!m_mainMenuSprite) m_mainMenuSprite = new MainMenuSprite();
			m_mainMenuSprite.x = (m_stage.stageWidth - Main.txtDebug.width) / 2;
			m_mainMenuSprite.y = m_stage.stageHeight / 2;
			if (!m_stage.contains(m_mainMenuSprite))
			{
				m_stage.addChild(m_mainMenuSprite);
			}
			
			m_mainMenuSprite.btnStart.addEventListener(MouseEvent.CLICK, buttonEventHandler);
			m_mainMenuSprite.btnExit.addEventListener(MouseEvent.CLICK, buttonEventHandler);
		}
		
		public function update():void
		{
			
		}
		
		public function uninitialize():void
		{
			
		}
		
		public function destroy():void
		{
			if (m_mainMenuSprite)
			{
				m_mainMenuSprite.btnStart.removeEventListener(MouseEvent.CLICK, buttonEventHandler);
				m_mainMenuSprite.btnExit.removeEventListener(MouseEvent.CLICK, buttonEventHandler);
				if (m_stage.contains(m_mainMenuSprite))
				{
					m_stage.removeChild(m_mainMenuSprite);
				}
				m_mainMenuSprite = null;
			}
			
			m_stage = null;
		}
		
		/* private functions */
		private function buttonEventHandler(me:MouseEvent):void
		{
			if (m_mainMenuSprite)
			{
				switch(me.target)
				{
					case m_mainMenuSprite.btnStart:
						GameStateManager.nextState = GameStateManager.uiGSM_GAMEPLAY;
						break;
						
					case m_mainMenuSprite.btnExit:
						fscommand("quit");
						break;
				}
			}
		}
	}

}
